package com.wechat.android.app.ui;

import android.content.Intent;

import com.easemob.cases.MessageHelper;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.wechat.android.app.R;
import com.wechat.android.app.db.UserHelper;
import com.wechat.android.app.ui.login.LoginActivity;
import com.wechat.android.app.utils.MyLog;

/**
 * Created by yang on 2018/1/18.
 * 闪屏页
 */

public class SplashActivity extends BaseActivity{
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initInjector() {
        UserHelper.getInstance().setIsLogined(initLoginState());
    }

    @Override
    protected void initViews() {
        intoMainActivity();
    }

    @Override
    protected void updateViews() {

    }

    /**
     * 每次开始登陆环信服务器
     */
    private void intoMainActivity() {
        MyLog.d("yang","======into MainActivity====");
        if (UserHelper.getInstance().isLogined()) {
            MessageHelper.getInstance().reset();
            MyLog.e("yang", UserHelper.getInstance().getUserInfoModel(this).getImKey() + "|" + UserHelper.getInstance().getUserInfoModel(this).getPassword());
            EMClient.getInstance().login(UserHelper.getInstance().getUserInfoModel(this).getImKey(), UserHelper.getInstance().getUserInfoModel(this).getPassword(), new EMCallBack() {//回调
                @Override
                public void onSuccess() {
                    MessageHelper.getInstance().getUserProfileManager().setCurrentUser(UserHelper.getInstance().getUserInfoModel(getApplicationContext()).getName(),
                            UserHelper.getInstance().getUserInfoModel(getApplicationContext()).getLogo());

                    EMClient.getInstance().groupManager().loadAllGroups();
                    EMClient.getInstance().chatManager().loadAllConversations();

                    MyLog.e("yang", "登录聊天服务器成功！" + UserHelper.getInstance().getUserInfoModel(SplashActivity.this).getImKey() + "|" + UserHelper.getInstance().getUserInfoModel(SplashActivity.this).getPassword());
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    finish();
                }

                @Override
                public void onProgress(int progress, String status) {
                    MyLog.d("yang","onProgress: status = " + status);
                }

                @Override
                public void onError(int code, String message) {
                    MyLog.d("yang", "登录聊天服务器失败！" + code + "|" + message);
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    finish();
                }
            });
        } else {
            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
            finish();
        }
    }
    //用户是否存在判断
    private boolean initLoginState() {
        if (UserHelper.getInstance().getUserInfoModel(this) == null ) {
            return false;
        } else {
            if (UserHelper.getInstance().getUserInfoModel(this).getImKey() == null ) {
                return false;
            }

            return true;
        }
    }
}
